using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class SelectRectHelper : NodeHelper
    {
        public Vector2 defaultRect;
        public List<SelectRectItem> list;

        public bool full;
        public RectTransform rect;
        public GridLayoutGroup grid;

        protected override BindDataType bindDataType => BindDataType.String;

        protected override void BindData(string s)
        {
            if (StringTools.IsEmpty(s)) return;
            var size = defaultRect;
            for (var i = 0; i < list.Count; i++)
                if (s == list[i].key)
                {
                    size = list[i].rect;
                    break;
                }

            SetRect(size);
        }

        private void SetRect(Vector2 size)
        {
            if (rect != null)
            {
                if (full)
                {
                    // rect.offsetMin = new Vector2(showFullRect.x, showFullRect.w);
                    // rect.offsetMax = new Vector2(showFullRect.z * -1, showFullRect.y * -1);
                }
                else
                {
                    rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
                    rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
                }
            }

            if (grid != null) grid.cellSize = size;
        }

        [Serializable]
        public class SelectRectItem
        {
            public string key;
            public Vector2 rect;
        }
    }
}